package cn.erp.app.system.controller;

import java.util.ArrayList;
import java.util.List;

import cn.erp.app.service.BaseInfoService;
import cn.erp.app.service.ProduceService;
import cn.erp.app.util.EuiRender;
import cn.erp.common.model.SvcBaseinfo;
import cn.erp.common.model.SvcMaterielSubdivision;
import cn.erp.common.model.SvcProduceCategory;

import com.jfinal.common.controller.BaseController;
import com.jfinal.ext.ret.ReqResult;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Page;

@ControllerBind(controllerKey = "/system/materiel", viewPath = "/system/materiel")
public class MaterielController extends BaseController {
	public void index() {
		List<SvcProduceCategory> produces = ProduceService.me.getAll();
		setAttr("produces", produces);
		List<SvcBaseinfo> baseInfo = BaseInfoService.me.getAllMaterielWithOutPro();
		setAttr("materiel", baseInfo);
		render("materiel_index.html");
	}

	public void getdata() {
		int pageNumber = getParaToInt("page", 1);
		int pageSize = getParaToInt("rows", 10);

		SvcMaterielSubdivision sbv = getModel(SvcMaterielSubdivision.class,
				"materiel");

		String where = "from svc_materiel_subdivision where 1=1  ";
		List<Object> objs = new ArrayList<Object>();
		if (sbv.getProduceId() != null) {
			where += " and produceId = ? ";
			objs.add(sbv.getProduceId());
		}
		if (sbv.getCategoryId() != null) {
			where += " and  categoryId = ? ";
			objs.add(sbv.getCategoryId());
		}
		if (sbv.getMaterielName() != null) {
			where += " and  MaterielName = ? ";
			objs.add(sbv.getMaterielName());
		}
		// System.out.println(objs.toArray());
		Page<SvcMaterielSubdivision> result = SvcMaterielSubdivision.dao
				.paginate(pageNumber, pageSize, "select * ", where,
						objs.toArray());
		render(EuiRender.page(result));
	}


	public void openadd() {
		long id= getParaToLong("id",0L);
		List<SvcProduceCategory> produces = ProduceService.me.getAll();
		setAttr("produces", produces);
		List<SvcBaseinfo> baseInfo = BaseInfoService.me.getAllMaterielWithOutPro();
		setAttr("materiel", baseInfo);
		SvcMaterielSubdivision pojo = new SvcMaterielSubdivision();
		if(id!=0L) {
			pojo = SvcMaterielSubdivision.dao.findById(id);
			
		}
		setAttr("pojo",pojo);
		render("materiel_add.html");
	}
	
	public void saveorupdate() {
		SvcMaterielSubdivision subdivision = getModel(
				SvcMaterielSubdivision.class, "");
		try {
			if (subdivision.getId() != null && subdivision.getId() != 0) {
				subdivision.update();
			} else {
				subdivision.save();
			}
			renderJson(ReqResult.ADD_SUCCESS);
			return;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		renderJson(ReqResult.ADD_ERROR);
	}

	public void delete() {
		String[] ids = getParaValues("ids");
		try {
			if (ids != null && !ids.equals("")) {
				for (String id : ids) {
					SvcMaterielSubdivision.dao.deleteById(id);
				}
			}
			renderJson(ReqResult.DEL_SUCCESS);
			return;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		renderJson(ReqResult.DEL_ERROR);

	}

	public void edit() {
		long id = getParaToLong("id");
		try {
			SvcMaterielSubdivision subDivision = SvcMaterielSubdivision.dao
					.findById(id);
			renderJson(subDivision);
			return;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		renderJson(ReqResult.ADD_ERROR);
	}
}
